iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 16
0
Modern Web

把前後分離製作的網站組起來系列 第 16

創造~REST APIs

  • 分享至 

  • xImage
  •  

https://ithelp.ithome.com.tw/upload/images/20201104/20119035CxSDvhjrAG.png
新增一個叫dao的package
再新增一個叫ProductRepository.java檔要選用interface
https://ithelp.ithome.com.tw/upload/images/20201104/20119035pij9fuCgIn.png

<Product,Long>的Product是Entity和Model表面上很類似,似乎都是反映資料狀態的物件。
而Long是Primary Key是指用來識別記錄的唯一性,它不可以重複及. 空值(Null)。


import com.huang.ecommerce.entity.Product;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ProductRepository extends JpaRepository<Product,Long> {
}

再新增一個叫ProductCategoryRepository.java檔要選用interface然後同上做JpaRepository<ProductCategory,Long> 要記得import ProductCategory再多加@RepositoryRestResource

其中@RepositoryRestResource(collectionResourceRel = "productCategory",path = "product-category")的productCategory是JSON entry而path = "product-category"


import com.huang.ecommerce.entity.ProductCategory;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;


@RepositoryRestResource(collectionResourceRel = "productCategory",path = "product-category")
public interface ProductCategoryRepository extends JpaRepository<ProductCategory,Long> {
}


https://ithelp.ithome.com.tw/upload/images/20201104/20119035tyABX09AsW.png

按SpringBootEcommerceApplication來RUN看看
結果發現一直有java11不能用的狀況

查了這篇https://stackoverflow.com/questions/54137286/error-java-invalid-target-release-11-intellij-idea/54215199

也試了用內建的jdk升級
https://ithelp.ithome.com.tw/upload/images/20201104/20119035sdqP7ah0HB.png
希望會成功~因為已經是重蓋地2次了QQ

結果卡在8080連不上~QQ給我點時間來debug...

解決方法:我是windows10
就是打開cmd
然後打上netstat -ano|findstr 8080
https://ithelp.ithome.com.tw/upload/images/20201104/201190355kN6awbZNZ.png

看看是被誰listening
到工作管理員->看服務->同樣編號的停止
https://ithelp.ithome.com.tw/upload/images/20201104/201190359XyrPyOklA.png
就出來了 http://localhost:8080/api
https://ithelp.ithome.com.tw/upload/images/20201104/20119035z4w7iz76kk.png

http://localhost:8080/api/products

https://ithelp.ithome.com.tw/upload/images/20201104/20119035ixairOdJSO.png


先繼續建好~新增一個叫config的package
再新增一個叫MyDataRestConfig.java檔


import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer;

public class MyDataRestConfig implements RepositoryRestConfigurer {
    
}

然後按右鍵Generate選implement Methods選第1個
https://ithelp.ithome.com.tw/upload/images/20201104/20119035hqnxFIcBrp.png


import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer;

public class MyDataRestConfig implements RepositoryRestConfigurer {

    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        
    }
}

在裡面打入"灰色"部分不是用打的
https://ithelp.ithome.com.tw/upload/images/20201104/20119035nZvofJpNw5.png


import com.huang.ecommerce.entity.Product;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer;
import org.springframework.http.HttpMethod;

public class MyDataRestConfig implements RepositoryRestConfigurer {

    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {

        HttpMethod[] theUnsupportedActions={HttpMethod.PUT,HttpMethod.POST,HttpMethod.DELETE};

        config.getExposureConfiguration()
            .forDomainType(Product.class)
                .withItemExposure(((metdata, httpMethods) -> httpMethods.disable(theUnsupportedActions)))
            .withCollectionExposure(((metdata, httpMethods) -> httpMethods.disable(theUnsupportedActions)));


    }


在最上面加上@Configuration


import com.huang.ecommerce.entity.Product;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer;
import org.springframework.http.HttpMethod;

@Configuration

public class MyDataRestConfig implements RepositoryRestConfigurer {

    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {

        HttpMethod[] theUnsupportedActions={HttpMethod.PUT,HttpMethod.POST,HttpMethod.DELETE};

        config.getExposureConfiguration()
            .forDomainType(Product.class)
                .withItemExposure(((metdata, httpMethods) -> httpMethods.disable(theUnsupportedActions)))
            .withCollectionExposure(((metdata, httpMethods) -> httpMethods.disable(theUnsupportedActions)));


    }
}


按SpringBootEcommerceApplication來RUN看看
結果卡在8080連不上~QQ給我點時間來debug...
https://ithelp.ithome.com.tw/upload/images/20201104/20119035YC5aKy1Vq5.png

解決方法:我是windows10
就是打開cmd
然後打上netstat -ano|findstr 8080
https://ithelp.ithome.com.tw/upload/images/20201104/201190355kN6awbZNZ.png

看看是被誰listening
到工作管理員->看服務->同樣編號的停止
https://ithelp.ithome.com.tw/upload/images/20201104/201190359XyrPyOklA.png

用POSTMAN看http://localhost:8080/api/products
https://ithelp.ithome.com.tw/upload/images/20201104/20119035XFaURvnqSf.png

下面就要
接續第11篇的ANGULAR

DEAR ALL 我們明天見/images/emoticon/emoticon08.gif


上一篇
然後~準備到REST APIs
下一篇
接續第11篇的ANGULAR
系列文
把前後分離製作的網站組起來30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言